<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Tuolaji <479923197@qq.com>
// +----------------------------------------------------------------------
namespace Product\Controller;

use Common\Controller\AdminbaseController;

class AdminAddressController extends AdminbaseController {

    protected $m_address;

    function _initialize() {
        parent::_initialize();
        $this->m_address = D("Product/Address");
    }

    /**
     * 商品分类管理
     */
    public function index(){

        $count=$this->m_address->count();
        $page = $this->page($count, 15);
        $list = $this->m_address->field("a.*,u.user_nicename")->alias("a")
            ->join("lz_users u ON u.id = a.uid")
            ->order("create_time desc")
            ->limit($page->firstRow , $page->listRows)
            ->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("list",$list);
        $this->display();
    }

    /**
     * 排序
     */
    public function listorders(){
        $status = parent::_listorders($this->address,'sort');
        if ($status) {
            $this->success("排序更新成功！");
        } else {
            $this->error("排序更新失败！");
        }
    }

    // 文章添加
    public function add(){
        $id = I("get.id",0,'intval');
        if(!empty($id)){
            $item=$this->address->where(array('term_id'=>$id))->find();
            $this->assign("item",$item);
        }

        $this->display();
    }

    // 文章添加提交
    public function add_post(){
        if (IS_POST) {
            $id = I("post.id");
            $insert = array(
                'title'=>I("post.title"),
                'sort'=>I("post.sort"),
            );
            if(empty($id)){
                $result =$this->address->add($insert);
            }else{
                $result = $this->address->where(array("id"=>$id))->save($insert);
            }
            if ($result) {
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }

        }
    }




    // 文章删除
    public function delete(){
        if(isset($_GET['id'])){
            $id = I("get.id",0,'intval');
            if ($this->address->where(array('id'=>$id))->delete() !==false) {
                $this->success("删除成功！");
            } else {
                $this->error("删除失败！");
            }
        }else{
            $this->error("参数有误！");
        }

    }

}